#include "Configuration.h"
#include "DirScanner.h"
#include <fstream>
#include <sstream>
#include <iostream>

using std::cout;
using std::endl;

using std::istringstream;
using std::ifstream;

Configuration* Configuration::getInstance()
{
    if(pCnof == nullptr)
    {
        pCnof = new Configuration("./conf/conf.conf");//测试中写死

    }
    return pCnof;
}

Configuration::Configuration(const string & filepath)
:_filepath(filepath) 
{
    
}

map<string, string> & Configuration::getConfigMap()
{
    std::ifstream ifs(_filepath, std::ios::in);
    string line;
    while(getline(ifs, line), ifs.good())
    {
        istringstream iss(line);
        string key, value;
        while(iss >> key >> value)
        {
            _configMap[key] = value;
        }
    }
    return _configMap;
}

Configuration::~Configuration()
{
    /* cout << "~Configuration()" << endl;  */
}

set<string> & Configuration::getStopWordList()
{
    for(auto & elem : _configMap)
    {
        if(elem.first == "stop")
        {
            string filepath = elem.second;
            DirScanner dirs;
            dirs(filepath);
            vector<string> tmp = dirs.files();
            for(auto & elem : tmp)
            {
                ifstream ifs;
                ifs.open(elem, std::ios::in);
                if(!ifs.good())
                {
                    perror("getStopWordList open ifstream");
                }
                string line;//因为停用词文件每行就一个词，所以直接insert就行
                while(getline(ifs,line), ifs.good())
                {
                    /* cout << "line " << line << endl; */
                    _stopWordList.insert(line);
                }
                ifs.close();
            }
        }
    }


    return _stopWordList;
}

